package com.dingdang.controller.car_management;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dingdang.data.Result;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.TbCarGoods;
import com.dingdang.mapper.TbCarGoodsMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("carGoods")
@Api("车辆信息管理接口")
public class CarGoosController
{
    @Autowired
    private TbCarGoodsMapper carGoodsMapper;

    @GetMapping
    @ApiOperation("车辆基本信息搜索")
    @ApiImplicitParam(name = "void",value = "空",required = true,paramType = "query")
    public Result getCarGoods()
    {
        List<TbCarGoods> carGoodsList = carGoodsMapper.selectList(null);
        return Result.response(ResultEnum.SUCCESS).setData(carGoodsList);
    }

    //管理员增加
    @PostMapping
    @ApiOperation("车辆基本信增加")
    @ApiImplicitParam(name = "TbCarGoods",value = "车辆基本信息实体类",required = true,paramType = "body")
    public Result insertAdmin(@RequestBody TbCarGoods carGoods)
    {
        int i = carGoodsMapper.insert(carGoods);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    //管理员回显
    @PostMapping("/toUpdate")
    @ApiOperation("车辆基本信回显")
    @ApiImplicitParam(name = "id",value = "车辆id",required = true,paramType = "query")
    public Result ToUpdateAdmin(@RequestParam("id") Long id)
    {
        TbCarGoods carGoods = new TbCarGoods();
        carGoods = carGoodsMapper.selectById(id);
        return Result.response(ResultEnum.SUCCESS).setData(carGoods);
    }

    //管理员修改
    @PutMapping
    @ApiOperation("车辆基本信修改")
    @ApiImplicitParam(name = "TbCarGoods",value = "车辆基本信息实体类",required = true,paramType = "body")
    public Result UpdateAdmin(@RequestBody TbCarGoods carGoods)
    {
        int i = carGoodsMapper.updateById(carGoods);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    //修改车俩售出状态
    @PutMapping("udpateStatus")
    @ApiOperation("修改是否已经删除")
    @ApiImplicitParam(name = "carId",value = "车辆id",required = true,paramType = "query")
    public void UpdateaInfo(@RequestParam("carId") Long carId)
    {
        LambdaUpdateWrapper<TbCarGoods> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(TbCarGoods::getCarId,carId).set(TbCarGoods::getIsDelete,"1");
        carGoodsMapper.update(null, wrapper);
    }


    @DeleteMapping()
    @ApiOperation("车辆基本信删除")
    @ApiImplicitParam(name = "id",value = "车辆id",required = true,paramType = "query")
    public Result deleteInfo(@RequestParam(value = "id") Long []ids)
    {
        List<Long> deleteBatch = new ArrayList<>();

        for (Long addr:ids)
        {
            deleteBatch.add(addr);
        }
        int i = carGoodsMapper.deleteBatchIds(deleteBatch);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @GetMapping("getIsDelete")
    @ApiOperation("车辆状态查询")
    @ApiImplicitParam(name = "void",value = "车辆id",required = true,paramType = "query")
    public boolean getIsDelete(Long carId){
        TbCarGoods tbCarGoods = carGoodsMapper.selectById(carId);
        return tbCarGoods.getIsDelete().equals("2");
    }


}
